#ifndef _UTILEX__LOCKQUEUE_H_
#define _UTILEX__LOCKQUEUE_H_

#include <crt_util.h>

template <class _Ty, class _Tr = default_safeptr> class CLockQueue : protected std::queue<_Ty>, public CAutoLock
{
    typedef std::queue<_Ty> _base;

public:
    CLockQueue() { }
    ~CLockQueue() { }

public:
    HRESULT push_queue(_Ty pNode)
    {
        rc_error(pNode != NULL, E_FAIL);

        SYNC_OBJ(this);
        this->_base::push(pNode);

        return S_OK;
    }

    HRESULT pop_queue(_Ty* pNode)
    {
        SYNC_OBJ(this);
        *pNode = this->_base::front();
        this->_base::pop();
        return S_OK;
    }

    HRESULT get_front(_Ty* pNode)
    {
        SYNC_OBJ(this);
        *pNode = this->_base::front();
        return S_OK;
    }

    HRESULT get_back(_Ty* pNode)
    {
        SYNC_OBJ(this);
        *pNode = this->_base::back();
        return S_OK;
    }

    UINT size()
    {
        SYNC_OBJ(this);
        return this->_base::size();
    }

    HRESULT auto_clear()
    {
        SYNC_OBJ(this);
        size_t loop = this->_base::size();
        while (loop != 0) {
            _Ty pNode = this->_base::front();
            this->_base::pop();
            m_tr.destroy(pNode);
            loop = this->_base::size();
        }

        return S_SUCCESS;
    }

private:
    _Tr m_tr;
};
#endif
